//
//  GroupMemberCell.m
//  Assistant
//
//  Created by Hunk on 12-2-28.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "GroupMemberCell.h"
#import <QuartzCore/QuartzCore.h>

@implementation GroupMemberCell
@synthesize delegate = _delegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) 
    {    
        // Person photo
        _personPhotoButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_personPhotoButton setFrame:CGRectMake(10.0, 2.0, 56.0, 56.0)];
        _personPhotoButton.layer.cornerRadius = CORNERRADIUS;
        _personPhotoButton.layer.masksToBounds = YES;
        [_personPhotoButton addTarget:self 
                               action:@selector(photoButtonHandle:) 
                     forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_personPhotoButton];
        
        // Person name
        _personNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(70.0, 0.0, 200.0, 30.0)];
        [_personNameLabel setBackgroundColor:[UIColor clearColor]];
        [_personNameLabel setTextAlignment:UITextAlignmentLeft];
        [_personNameLabel setTextColor:[UIColor blackColor]];
        [_personNameLabel setFont:[UIFont boldSystemFontOfSize:20]];
        [self.contentView addSubview:_personNameLabel];
        
        
        _personPhoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(70.0, 30.0, 200.0, 30.0)];
        [_personPhoneLabel setBackgroundColor:[UIColor clearColor]];
        [_personPhoneLabel setTextAlignment:UITextAlignmentLeft];
        [_personPhoneLabel setTextColor:[UIColor blackColor]];
        [_personPhoneLabel setFont:[UIFont systemFontOfSize:16]];
        [self.contentView addSubview:_personPhoneLabel];
    }
    return self;
}

- (void)setDelegate:(id<GroupMemberCellDelegate>)delegate
{
    if(delegate != _delegate)
    {
        [_delegate release];
        
        _delegate = [delegate retain];
    }
}

#pragma mark -
#pragma mark setGroupMember
- (void)setGroupMember:(AddressBookPerson *)addressBookPerson
{
    if(_addressBookPerson != addressBookPerson)
    {
        [_addressBookPerson release];
        
        _addressBookPerson = [addressBookPerson retain];
    }

    UIImage *personPhoto = [addressBookPerson personPhoto];
    if(nil != personPhoto)
    {
        [_personPhotoButton setImage:personPhoto forState:UIControlStateNormal];
    }
    else
    {
        [_personPhotoButton setImage:[UIImage imageNamed:@"icon_action.png"] forState:UIControlStateNormal];
    }
    
    [_personNameLabel setText:[addressBookPerson personName]];
    
    NSString *mobileString = [[addressBookPerson personPhone] objectForKey:MOBILE_LABEL];
    if(mobileString != nil)
    {
        [_personPhoneLabel setText:mobileString];
    }
    else
    {
        [_personPhoneLabel setText:[[addressBookPerson personPhone] objectForKey:IPHONE_LABEL]];
    }
}

#pragma mark -
#pragma mark photoButtonHandle
- (void)photoButtonHandle:(id)sender
{
    if([_delegate respondsToSelector:@selector(groupMemberCell:didSelectedPerson:)])
    {
        [_delegate groupMemberCell:self didSelectedPerson:_addressBookPerson];
    }
}

- (void)dealloc
{   
    SAFE_RELEASE(_addressBookPerson)
    SAFE_RELEASE(_personNameLabel)
    SAFE_RELEASE(_personPhoneLabel)
    SAFE_RELEASE(_delegate)
    
    [super dealloc];
}

@end
